package com.bcx.crm.java.proxy.jdkProxy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by ys on 2018/1/26.
 */
public class UserDaoProxy implements InvocationHandler {
    private Object target;
    public UserDaoProxy(Object target){
        this.target = target;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String res =(String) method.invoke(target, args);
        return res;
    }

    public static void main(String[] args) throws Throwable {
        Class<?> proxyClass = Proxy.getProxyClass(IUserDao.class.getClassLoader(), IUserDao.class);
        Constructor<?> constructor = proxyClass.getConstructor(InvocationHandler.class);
        IUserDao iUserDao =(IUserDao) constructor.newInstance(new UserDaoProxy(new UserDaoImpl()));
        String zhang = iUserDao.save("zhang");
        System.out.println(zhang);
    }
}
